<template>
  <van-swipe-cell :right-width="userInfo.userType === 0 ? 72 : 0">
    <van-cell-group>
      <div class="body">
        <div
          class="pic"
          @click="$emit('toDetail')">
          <image
            :src="
              itemInfo.imgUrl ? itemInfo.imgUrl : '../../static/icon/图片.svg'
            "
            mode="aspectFill" />
        </div>
        <div
          v-if="isPopular"
          class="title"
          @click="$emit('toDetail')">
          {{ itemInfo.name }}
        </div>
        <div
          v-else
          class="title"
          @click="$emit('toDetail')">
          {{ itemInfo.title }}
        </div>
        <div
          class="foot"
          v-if="!isPopular">
          <div class="comment">
            <image
              src="@/static/icon/评论.svg"
              mode="scaleToFill" />
            <text>{{ itemInfo.commentsCount }}</text>
          </div>
          <div class="like">
            <image
              @click="likeItem"
              v-if="!isLiked"
              src="@/static/icon/点赞.svg"
              mode="scaleToFill" />
            <image
              v-if="isLiked"
              src="@/static/icon/已点赞.svg"
              mode="scaleToFill" />
            <text>{{ likeCount }}</text>
          </div>
        </div>
      </div>
    </van-cell-group>
    <view
      slot="right"
      @click="toDelete"
      class="van-swipe-cell__delete">
      删除
    </view>
  </van-swipe-cell>
</template>

<script>
import { mapState } from "vuex";

export default {
  computed: {
    ...mapState(["userInfo"]),
  },
  props: {
    itemInfo: {
      type: Object,
      required: true,
    },
    isPopular: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isLiked: false,
      likeCount: this.itemInfo.likes,
    };
  },
  methods: {
    likeItem() {
      this.isLiked = !this.isLiked;
      this.likeCount += 1;
      this.$emit("likeItem");
    },
    toDelete() {
      this.$emit("toDelete");
    },
  },
  // 解决在微信小程序中 样式穿透无效的问题
  options: {
    styleIsolation: "shared",
  },
};
</script>

<style lang="scss" scoped>
.body {
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0px 4px 16px 0px #edeef0;
  gap: 8px;
  margin-bottom: 16px;
}

.pic {
  height: 200px;
  margin: 8px;
  image {
    width: 100%;
    height: 200px;
    border-radius: 12px;
  }
}

.title {
  font-size: 18px;
  font-weight: bold;
  margin: 0px 16px 12px 16px;
}

.foot {
  margin: 0px 16px 12px 16px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  image {
    width: 24px;
    height: 24px;
  }
  .comment {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-right: 12px;
  }
  .like {
    display: flex;
    align-items: center;
    gap: 4px;
  }
}

::v-deep .van-swipe-cell__right {
  display: flex;
  align-items: center;
  padding-left: 12px;
}
.van-swipe-cell__edit {
  width: 60px;
  height: 30px;
  border-radius: 6px;
  text-align: center;
  background-color: #5282ff;
  color: #ffffff;
  font-size: 14px;
  font-weight: 600;
  line-height: 30px;
  margin-right: 6px;
}
.van-swipe-cell__delete {
  width: 60px;
  height: 30px;
  border-radius: 6px;
  text-align: center;
  background-color: #ee0a24;
  color: #ffffff;
  font-size: 14px;
  font-weight: 600;
  line-height: 30px;
  margin-right: 6px;
}
</style>
